package com.hwq.sql.tool.enums;

import java.lang.reflect.Field;

public enum DataType {

    INT("INT", "INT"),
    NUMBER("NUMBER", "DECIMAL"),
    VARCHAR("VARCHAR2", "VARCHAR"),
    CHAR("CHAR", "CHAR"),
    DATE("DATE", "DATETIME"),
    BLOB("CLOB", "TEXT")
    ;
    private final String oracle;
    private final String mysql;

    DataType(String oracle, String mysql) {
        this.oracle = oracle;
        this.mysql = mysql;
    }

    public String oracle() {
        return oracle;
    }

    public String mysql() {
        return mysql;
    }

    /**
     * 根据 oracle 数据库 数据类型获取 类型映射枚举
     * @param type 数据类型
     */
    public static DataType get(String type, String dbType) {
        try {
            Class<DataType> tClass = DataType.class;
            Field field = tClass.getDeclaredField(dbType);
            field.setAccessible(true);
            for (DataType data : DataType.values()) {
                Object value = field.get(data);
                if (type.equals(value)) {
                    return data;
                }
            }
            throw new RuntimeException(type + " 是不支持的数据类型");
        } catch (NoSuchFieldException | IllegalAccessException ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex.getMessage());
        }
    }
}
